/* 
 * FLAC 1.2.1 bitreader.c
 * implementation for NDS
 */

#include "audioreader.h"

#define MIN(x,y) ((x)<(y)?(x):(y))
#define MAX(x,y) ((x)>(y)?(x):(y))

const uint8_t AudioReader::byte_to_unary_table[256] = {
	8, 7, 6, 6, 5, 5, 5, 5, 4, 4, 4, 4, 4, 4, 4, 4,
	3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
	2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
	2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
	1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
	1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
	1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
	1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
};

// ----------------------------------------------------------------------------
inline uint32_t AudioReader::count_zero_msbs(uint32_t word)
// ----------------------------------------------------------------------------
{
	return (word <= 0xFFFF) ?
	       ( (word <= 0xFF) ? byte_to_unary_table[word]+24 : byte_to_unary_table[word>>8]+16 ) :
	       ( (word <= 0xFFFFFF) ? byte_to_unary_table[word>>16]+8 : byte_to_unary_table[word>>24] );
}

// ----------------------------------------------------------------------------
inline void AudioReader::swap_be_word(uint32_t *word)
// ----------------------------------------------------------------------------
{
	*word = (((*word)<<8)&0xFF00FF00) | (((*word)>>8)&0x00FF00FF);
	*word = ((*word)>>16) | ((*word)<<16);
}

// ----------------------------------------------------------------------------
inline void AudioReader::swap_be_block(uint32_t *start, uint32_t len)
// ----------------------------------------------------------------------------
{
	for (uint32_t i=0; i<len; ++i)
	{
		start[i] = ((start[i]<<8)&0xFF00FF00) | ((start[i]>>8)&0x00FF00FF);
		start[i] = (start[i]>>16) | (start[i]<<16);
	}
}

// ----------------------------------------------------------------------------
inline bool AudioReader::refill_buffer()
// ----------------------------------------------------------------------------
{
	uint32_t start, end;
	size_t l_bytes;
	uint8_t *target;

	/* first shift the unconsumed buffer data toward the front as much as possible */
	if (consumed_words > 0)
	{
		start = consumed_words;
		end = words + (bytes ? 1:0);
		#ifdef ARM9
		swiFastCopy((buffer+start), buffer, ((end-start) | COPY_MODE_WORD | COPY_MODE_COPY));
		#else
		memmove(buffer, buffer+start, ((end-start)<<2));
		#endif
		words -= start;
		consumed_words = 0;
	}
	/* set the target for reading, taking into account word alignment and endianess */
	l_bytes = ((capacity - words) << 2) - bytes;
	if (l_bytes == 0)
	{
		return false; /* no space left, buffer is too small */
	}
	target = ((uint8_t *)(buffer + words)) + bytes;

	/* byteswap */
	if (bytes)
	{
		swap_be_word(&buffer[words]);
	}
	/* read data */
	l_bytes = fread(target, 1, l_bytes, file);
	if (l_bytes == 0)
	{
		return false;
	}
	/* byteswap */
	end = ((words<<2) + bytes + l_bytes + 3) >> 2;
	start = words;
	swap_be_block((buffer + start), (end - start));
	/* update reader values */
	end = (words<<2) + bytes + l_bytes;
	#ifdef ARM9
	swiDivMod(end, 4, (int *)&words, (int *)&bytes);
	#else
	words = end / 4;
	bytes = end % 4;
	#endif
	return true;
}

// ----------------------------------------------------------------------------
bool AudioReader::init(std::string filename)
// ----------------------------------------------------------------------------
{
	if (file != NULL) fclose(file);
	file = fopen(filename.data(), "r");
	if (file == NULL)
	{
		return false;
	}
	words = bytes = 0;
	consumed_words = consumed_bits = 0;
	capacity = (AUDIOBUFFERSIZE>>2);
	return refill_buffer();
}

// ----------------------------------------------------------------------------
bool AudioReader::clear()
// ----------------------------------------------------------------------------
{
	words = bytes = 0;
	consumed_words = consumed_bits = 0;
	return true;
}

// ----------------------------------------------------------------------------
inline uint32_t AudioReader::bits_left_for_byte_alignment()
// ----------------------------------------------------------------------------
{
	return 8 - (consumed_bits & 7);
}

// ----------------------------------------------------------------------------
inline uint32_t AudioReader::get_input_bits_unconsumed()
// ----------------------------------------------------------------------------
{
	return ((words - consumed_words)<<5) + (bytes<<3) - consumed_bits;
}

// ----------------------------------------------------------------------------
bool AudioReader::read_uint32_be(uint32_t *val, uint32_t bits)
// ----------------------------------------------------------------------------
{
	/* asserts */
	if (bits > 32) return false;
	if ((capacity<<6) < bits) return false;
	if (consumed_words > words) return false;

	if (bits == 0)
	{
		*val = 0;
		return true;
	}

	while (((words-consumed_words)<<5) + (bytes<<3) - consumed_bits < bits)
	{
		if (!refill_buffer()) return false;
	}
	if (consumed_words < words)
	{
		/* if we've not consumed up to a partial tail words... */
		if (consumed_bits)
		{
			/* this also works when consumed_bits==0, it's just a little
			 * slower than necessary for that case */
			const uint32_t n = 32 - consumed_bits;
			const uint32_t word = buffer[consumed_words];
			if (bits < n)
			{
				*val = (word & (0xFFFFFFFF >> consumed_bits)) >> (n-bits);
				consumed_bits += bits;
				return true;
			}
			*val = word & (0xFFFFFFFF >> consumed_bits);
			bits -= n;
			//crc16_update_word(word);
			consumed_words++;
			consumed_bits = 0;
			if (bits)
			{
				/* if there are still bits left to read, there have to be less than 32
				 * so they will all be in the next word */
				*val <<= bits;
				*val |= (buffer[consumed_words] >> (32-bits));
				consumed_bits = bits;
			}
			return true;
		}
		else
		{
			const uint32_t word = buffer[consumed_words];
			if (bits < 32)
			{
				*val = word >> (32-bits);
				consumed_bits = bits;
				return true;
			}
			/* 'bits' must be 32 here */
			*val = word;
			//crc16_update_word(word);
			consumed_words++;
			return true;
		}
	}
	else
	{
		/* in this case we're starting our read at a partial tail word;
		 * the reader has guaranteed that we have at least 'bits' bits
		 * available to read, which makes this case simpler. */
		if (consumed_bits)
		{
			/* this also works when consumed_bits==0, it's just a little
			 * slower than necessary for that case */
			if (consumed_bits + bits > (bytes<<3)) return false;
			*val = (buffer[consumed_words] & (0xFFFFFFFF >> consumed_bits)) >> (32-consumed_bits-bits);
			consumed_bits += bits;
			return true;
		}
		else
		{
			*val = buffer[consumed_words] >> (32-bits);
			consumed_bits += bits;
			return true;
		}
	}
}

// ----------------------------------------------------------------------------
bool AudioReader::read_int32_be(int32_t *val, uint32_t bits)
// ----------------------------------------------------------------------------
{
	if (!read_uint32_be((uint32_t *)val, bits)) return false;
	/* sign-extend */
	*val <<= (32-bits);
	*val >>= (32-bits);
	return true;
}

// ----------------------------------------------------------------------------
bool AudioReader::read_uint64_be(uint64_t *val, uint32_t bits)
// ----------------------------------------------------------------------------
{
	uint32_t hi, lo;

	if (bits > 32)
	{
		if (!read_uint32_be(&hi, bits-32)) return false;
		if (!read_uint32_be(&lo, 32)) return false;
		*val = hi;
		*val <<= 32;
		*val |= lo;
	}
	else
	{
		if (!read_uint32_be(&lo, bits)) return false;
		*val = lo;
	}
	return true;
}

// ----------------------------------------------------------------------------
bool AudioReader::read_uint32_le(uint32_t *val, uint32_t bits)
// ----------------------------------------------------------------------------
{
	uint32_t x8, x32 = 0;

	if (bits > 32)
	{
		return false;
	}
	if (bits > 24)
	{
		if (!read_uint32_be(&x32, 8)) return false;
		if (!read_uint32_be(&x8, 8)) return false;
		x32 |= (x8 << 8);		
		if (!read_uint32_be(&x8, 8)) return false;
		x32 |= (x8 << 16);		
		if (!read_uint32_be(&x8, bits-24)) return false;
		x32 |= (x8 << 24);		
	}
	else if (bits > 16)
	{
		if (!read_uint32_be(&x32, 8)) return false;
		if (!read_uint32_be(&x8, 8)) return false;
		x32 |= (x8 << 8);		
		if (!read_uint32_be(&x8, bits-16)) return false;
		x32 |= (x8 << 16);		
	}
	else if (bits > 8)
	{
		if (!read_uint32_be(&x32, 8)) return false;
		if (!read_uint32_be(&x8, bits-8)) return false;
		x32 |= (x8 << 8);		
	}
	else if (bits > 0)
	{
		if (!read_uint32_be(&x32, bits)) return false;
	}

	*val = x32;
	return true;
}

/*
// ----------------------------------------------------------------------------
bool AudioReader::read_int32_le(int32_t *val, uint32_t bits)
// ----------------------------------------------------------------------------
{
	int32_t x32;
	if (!read_int32_be(&x32, bits)) return false;
	swap_be_word(&x32);
	*val = x32;
}
*/

// ----------------------------------------------------------------------------
bool AudioReader::skip_bits(uint32_t bits)
// ----------------------------------------------------------------------------
{
	if (bits > 0)
	{
		const uint32_t n = consumed_bits & 7;
		uint32_t m;
		uint32_t x;

		if (n != 0)
		{
			m = MIN(8-n, bits);
			if (!read_uint32_be(&x, m)) return false;
			bits -= m;
		}
		m = bits / 8;
		if (m > 0)
		{
			if (!skip_byte_block(m, false)) return false;
			bits %= 8;
		}
		if (bits > 0)
		{
			if (!read_uint32_be(&x, bits)) return false;
		}
	}

	return true;
}

// ----------------------------------------------------------------------------
bool AudioReader::skip_byte_block(uint32_t nvals, bool align)
// ----------------------------------------------------------------------------
{
	uint32_t x;

	if (align && (consumed_bits & 7) != 0)
	{
		skip_bits(8-(consumed_bits & 7));
	}

	/* step 1: skip over partial head word to get word aligned */
	while (nvals && consumed_bits)
	{
		/* i.e. run until we read 'nvals' bytes or we hit the end of the head word */
		if (!read_uint32_be(&x, 8)) return false;
		nvals--;
	}
	if (0 == nvals) return true;

	/* step 2: skip whole words in chunks */
	while (nvals >= 4)
	{
		if (consumed_words < words)
		{
			consumed_words++;
			nvals -= 4;
		}
		else if (!refill_buffer()) return false;
	}

	/* step 3: skip any remainder from partial tail bytes */
	while (nvals)
	{
		if (!read_uint32_be(&x, 8)) return false;
		nvals--;
	}

	return true;
}

// ----------------------------------------------------------------------------
bool AudioReader::read_byte_block(uint8_t *val, uint32_t nvals, bool align)
// ----------------------------------------------------------------------------
{
	uint32_t x;

	if (align && (consumed_bits & 7) != 0)
	{
		skip_bits(8-(consumed_bits & 7));
	}

	/* step 1: read from partial head word to get word aligned */
	while (nvals && consumed_bits)
	{
		/* i.e. run until we read 'nvals' bytes or we hit the end of the head word */
		if (!read_uint32_be(&x, 8)) return false;
		*val++ = (uint8_t)x;
		nvals--;
	}
	if (0 == nvals) return true;

	/* step 2: read whole words in chunks */
	while (nvals >= 4)
	{
		if (consumed_words < words)
		{
			const uint32_t word = buffer[consumed_words++];
			val[0] = (uint8_t)(word >> 24);
			val[1] = (uint8_t)(word >> 16);
			val[2] = (uint8_t)(word >> 8);
			val[3] = (uint8_t)word;
			val += 4;
			nvals -= 4;
		}
		else if (!refill_buffer()) return false;
	}

	/* step 3: read any remainder from partial tail bytes */
	while (nvals)
	{
		if (!read_uint32_be(&x, 8)) return false;
		*val++ = (uint8_t)x;
		nvals--;
	}

	return true;
}

// ----------------------------------------------------------------------------
bool AudioReader::read_unary_unsigned(uint32_t *val)
// ----------------------------------------------------------------------------
{
	uint32_t i;

	*val = 0;
	while (1)
	{
		while (consumed_words < words)
		{
			/* if we've not consumed up to a partial tail word... */
			uint32_t b = buffer[consumed_words] << consumed_bits;
			if (b)
			{
				i = count_zero_msbs(b);
				*val += i;
				i++;
				consumed_bits += i;
				if (consumed_bits >= 32) /* faster way of testing == */
				{
					//crc16_update_word(buffer[consumed_words]);
					consumed_words++;
					consumed_bits = 0;
				}
				return true;
			}
			else
			{
				*val += 32 - consumed_bits;
				//crc16_update_word(buffer[consumed_words];
				consumed_words++;
				consumed_bits = 0;
				/* didn't find stop bit yet, have to keep going... */
			}
		}
		/* at this point we've eaten up all the whole words; have to try
		 * reading through any tail bytes before calling the read callback.
		 * this is a repeat of the above logic adjusted for the fact we
		 * don't have a whole word. */
		if (bytes)
		{
			const uint32_t end = bytes<<3;
			uint32_t b = (buffer[consumed_words] & (0xFFFFFFFF << (32-end))) << consumed_bits;
			if (b)
			{
				i = count_zero_msbs(b);
				*val += i;
				i++;
				consumed_bits += i;
				if (consumed_bits >= 32) return false;
				return true;
			}
			else
			{
				*val += end - consumed_bits;
				consumed_bits += end;
				if (consumed_bits >= 32) return false;
				/* didn't find stop bit yet, have to keep going... */
			}
		}
		if (!refill_buffer()) return false;
	}
}

// ----------------------------------------------------------------------------
// on return, if *val = 0xFFFFFFFF then the UTF-8 sequence was invalid,
// but the return value will be true
bool AudioReader::read_utf8_uint32(uint32_t *val, uint8_t *raw, uint32_t *rawlen)
// ----------------------------------------------------------------------------
{
	uint32_t v = 0;
	uint32_t x;
	uint32_t i;

	if (!read_uint32_be(&x, 8)) return false;
	if (raw)
	{
		raw[(*rawlen)++] = (uint8_t)x;
	}
	if (!(x & 0x80))
	{
		/* 0xxxxxxx */
		v = x;
		i = 0;
	}
	else if (x & 0xC0 && !(x & 0x20))
	{
		/* 110xxxxx */
		v = x & 0x1F;
		i = 1;
	}
	else if (x & 0xE0 && !(x & 0x10))
	{
		/* 1110xxxx */
		v = x & 0x0F;
		i = 2;
	}
	else if (x & 0xF0 && !(x & 0x08))
	{
		/* 11110xxx */
		v = x & 0x07;
		i = 3;
	}
	else if (x & 0xF8 && !(x & 0x04))
	{
		/* 111110xx */
		v = x & 0x03;
		i = 4;
	}
	else if (x & 0xFC && !(x & 0x02))
	{
		/* 1111110x */
		v = x & 0x01;
		i = 5;
	}
	else
	{
		*val = 0xFFFFFFFF;
		return true;
	}

	for (; i; i--)
	{
		if (!read_uint32_be(&x, 8)) return false;
		if (raw)
		{
			raw[(*rawlen)++] = (uint8_t)x;
		}
		if (!(x & 0x80) || (x & 0x40))
		{
			/* 10xxxxxx */
			*val = 0xFFFFFFFF;
			return true;
		}
		v <<= 6;
		v |= (x & 0x3F);
	}
	*val = v;
	return true;
}

// ----------------------------------------------------------------------------
// on return, if *val = 0xFFFFFFFFFFFFFFFF then the UTF-8 sequence was invalid,
// but the return value will be true
bool AudioReader::read_utf8_uint64(uint64_t *val, uint8_t *raw, uint32_t *rawlen)
// ----------------------------------------------------------------------------
{
	uint64_t v = 0;
	uint32_t x;
	uint32_t i;

	if (!read_uint32_be(&x, 8)) return false;

	if (raw)
	{
		raw[(*rawlen)++] = (uint8_t)x;
	}
	if (!(x & 0x80))
	{
		/* 0xxxxxxx */
		v = x;
		i = 0;
	}
	else if (x & 0xC0 && !(x & 0x20))
	{
		/* 110xxxxx */
		v = x & 0x1F;
		i = 1;
	}
	else if (x & 0xE0 && !(x & 0x10))
	{
		/* 1110xxxx */
		v = x & 0x0F;
		i = 2;
	}
	else if (x & 0xF0 && !(x & 0x08))
	{
		/* 11110xxx */
		v = x & 0x07;
		i = 3;
	}
	else if (x & 0xF8 && !(x & 0x04))
	{
		/* 111110xx */
		v = x & 0x03;
		i = 4;
	}
	else if (x & 0xFC && !(x & 0x02))
	{
		/* 1111110x */
		v = x & 0x01;
		i = 5;
	}
	else if (x & 0xFE && !(x & 0x01))
	{
		/* 11111110 */
		v = 0;
		i = 6;
	}
	else
	{
		((uint32_t *)val)[0] = 0xFFFFFFFF;
		((uint32_t *)val)[1] = 0xFFFFFFFF;
		return true;
	}

	for (; i; i--)
	{
		if (!read_uint32_be(&x, 8)) return false;
		if (raw)
		{
			raw[(*rawlen)++] = (uint8_t)x;
		}
		if (!(x & 0x80) || (x & 0x40))
		{
			/* 10xxxxxx */
			((uint32_t *)val)[0] = 0xFFFFFFFF;
			((uint32_t *)val)[1] = 0xFFFFFFFF;
			return false;;
		}
		v <<= 6;
		v |= (x & 0x3F);
	}
	*val = v;
	return true;
}

// ----------------------------------------------------------------------------
bool AudioReader::seek(long pos)
// ----------------------------------------------------------------------------
{
	fseek(file, pos, SEEK_SET);
	words = bytes = 0;
	consumed_words = consumed_bits = 0;
	return refill_buffer();
}

// ----------------------------------------------------------------------------
bool AudioReader::eof()
// ----------------------------------------------------------------------------
{
	return feof(file);
}

// ----------------------------------------------------------------------------
long AudioReader::tell()
// ----------------------------------------------------------------------------
{
	return ftell(file) - (capacity<<2) + ((consumed_words<<2) + ((consumed_bits == 0) ? 0 : (consumed_bits / 8)));
}

/* ========================================================================== *
 * ======================== FLAC-related methods ============================ *
 * ========================================================================== */

// ----------------------------------------------------------------------------
bool AudioReader::read_rice_signed(int32_t *val, uint32_t parameter)
// ----------------------------------------------------------------------------
{
	uint32_t lsbs = 0, msbs = 0;
	uint32_t uval;

	if (parameter > 31) return false;

	/* read the unary MSBs and end bit */
	if (!read_unary_unsigned(&msbs)) return false;
	/* read the binary LSBs */
	if (!read_uint32_be(&lsbs, parameter)) return false;

	/* compose the value */
	uval = (msbs << parameter) | lsbs;
	if (uval & 1)
	{
		*val = -((int32_t)(uval >> 1)) - 1;
	}
	else
	{
		*val = (int32_t)(uval >> 1);
	}

	return true;
}

// ----------------------------------------------------------------------------
bool AudioReader::read_rice_signed_block(int32_t *vals, uint32_t nvals, uint32_t parameter)
// ----------------------------------------------------------------------------
{
	/* magic for reading rice-coded residuals in FLAC */

	uint32_t i;
	uint32_t uval = 0;

	/* try to get this.consumed_words and this.consumed_bits into register
	 * must remeber to flush them back before calling other functions that
	 * use them, and before returning */
	register unsigned cwords;
	register unsigned cbits;
	unsigned ucbits; /* keep track of the number of unconsumed bits in the buffer */

	if (parameter >= 32) return false;

	if (nvals == 0) return true;

	cbits = consumed_bits;
	cwords = consumed_words;
	ucbits = ((words-cwords)<<5) + (bytes<<3) - cbits;

	while (1)
	{
		/* read unary part */
		while (1)
		{
			while (cwords < words)
			{
				/* if we've not consumed up to a partial tail word... */
				uint32_t b = buffer[cwords] << cbits;
				if (b)
				{
					i = count_zero_msbs(b);
					uval += i;
					cbits += i;
					cbits++; /* skip over stop bit */
					if (cbits >= 32) /* faster way of testing == */
					{
						//crc16_update_word(buffer[cwords]);
						cwords++;
						cbits = 0;
					}
					goto break1;
				}
				else
				{
					uval += 32 - cbits;
					//crc16_update_word(buffer[cwords]);
					cwords++;
					cbits = 0;
					/* didn't find stop bit yet, have to keep going... */
				}
			}
			/* at this point we've eaten up all the whole words; have to try
			 * reading through any tail bytes before calling the read callback.
			 * this is a repeat of the above logic adjusted for the fact we
			 * don't have a whole word. */
			if (bytes)
			{
				const uint32_t end = (bytes<<3);
				uint32_t b = (buffer[cwords] & ~(0xFFFFFFFF >> end)) << cbits;
				if (b)
				{
					i = count_zero_msbs(b);
					uval += i;
					cbits += i;
					cbits++; /* skip over stop bit */
					if (cbits >= 32) return false;
					goto break1;
				}
				else
				{
					uval += end - cbits;
					cbits += end;
					if (cbits >= 32) return false;
					/* didn't find stop bit yet, have to keep going... */
				}
			}
			/* flush registers and read; refill_buffer() does
			 * not toush consumed_bits at all but we still need to set
			 * it in case it fails and we have to return false. */
			consumed_bits = cbits;
			consumed_words = cwords;
			if (!refill_buffer()) return false;
			cwords = consumed_words;
			ucbits = ((words-cwords)<<5) + (bytes<<3) - cbits + uval;
			/* + uval to offset our count by the # of unary bits already
			 * consumed before the read, because we will add these back
			 * in all at once at break1 */
		}
break1:
		ucbits -= uval;
		ucbits--; /* account for stop bit */

		/* read binary part */
		if (cwords > words) return false;

		if (parameter)
		{
			while (ucbits < parameter)
			{
				/* flush registers and read; refill_buffer() does
				 * not toush consumed_bits at all but we still need to set
				 * it in case it fails and we have to return false. */
				consumed_bits = cbits;
				consumed_words = cwords;
				if (!refill_buffer()) return false;
				cwords = consumed_words;
				ucbits = ((words-cwords)<<5) + (bytes<<3) - cbits;
			}
			if (cwords < words)
			{
				/* if we've not consumed up to a partial tail word... */
				if (cbits)
				{
					/* this also works when consumed_bits==0, it's just slower than necessary for that case */
					const uint32_t n = 32 - cbits;
					const uint32_t word = buffer[cwords];
					if (parameter < n)
					{
						uval <<= parameter;
						uval |= (word & (0xFFFFFFFF >> cbits)) >> (n-parameter);
						cbits += parameter;
					}
					else
					{
						uval <<= n;
						uval |= word & (0xFFFFFFFF >> cbits);
						//crc16_update_word(word);
						cwords++;
						cbits = parameter - n;
						if (cbits)
						{
							/* parameter > n, i.e. if there are still bits left
							 * to read, there have to be less than 32 so they
							 * will all be in the next word */
							uval <<= cbits;
							uval |= (buffer[cwords] >> (32-cbits));
						}
					}
				}
				else
				{
					cbits = parameter;
					uval <<= parameter;
					uval |= buffer[cwords] >> (32-cbits);
				}
			}
			else
			{
				/* in this case we're starting our read at a partial tail word;
				 * the reader has guaranteed that we have at least 'parameter'
				 * bits available to read, which makes this case simler. */
				uval <<= parameter;
				if (cbits)
				{
					/* this also works when consumed_bits==0, it's just a little
					 * slower than necessary in that case */
					if (cbits + parameter > (bytes<<3)) return false;
					uval |= (buffer[cwords] & (0xFFFFFFFF >> cbits)) >> (32-cbits-parameter);
					cbits += parameter;
				}
				else
				{
					cbits = parameter;
					uval |= buffer[cwords] >> (32-cbits);
				}
			}
		}

		ucbits -= parameter;

		/* compose the value */
		*vals = (int32_t)(uval >> 1 ^ -(int32_t)(uval & 1));

		/* are we done? */
		--nvals;
		if (nvals == 0)
		{
			consumed_bits = cbits;
			consumed_words = cwords;
			return true;
		}

		uval = 0;
		++vals;
	}
}

